const fs = require('fs')
const path = require('path')
const axios = require('axios')
const { downloadDir, total } = require("./helper")
const { parse } = require("./parse")

if (!fs.existsSync(downloadDir)) {
    // fs.rmdirSync(downloadDir)
    fs.mkdirSync(downloadDir)
}



const url = "https://filesignatures.net/index.php?page=all&order=EXT&alpha="


function delay (time) {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve()
        }, time)
    })
}

async function getData (page) {
    if (!page) page = 1
    if (page > total) return;
    if (page !== 1) {
        await delay(500)
    }

    console.log(`正在拉取第${page}页数据`)
    const result = await axios.get(url, {
        params: {
            currentpage: page
        },
        headers: {
            ContentType: "text/html",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
        },
    })
    const { data: html } = result
    console.log(`拉取第${page}页数据成功\n`)

    await getData(page + 1)
    fs.writeFileSync(path.join(downloadDir, `${page}.html`), html)
}

// console.log("正在拉取数据")
// getData().then(async () => {
//     console.log("拉取数据成功\n\n\n\n")
//     await parse()
// })


parse()